import os
mydir = os.path.normpath(os.path.join(os.path.dirname( __file__),'..'))
import gtk

__all__ = ["generic_data_error", "get_warning_dialog", "get_error_dialog", 
           "get_info_dialog", "get_save_dialog", "generic_data_error",
           "mandatory_data_error",
           "search"]

# Directory for glade files
GLDFILESDIR = os.path.join(mydir, "ui", "glade")

# Mapping glade files to the name of the top level component
GLDFILES = dict(login_window = "login_window.glade",
                main_window = "main_window.glade",
                manage_user_frame = "manage_user_frame.glade",
                manage_author_frame = "manage_author_frame.glade",
                manage_book_frame = "manage_book_frame.glade",
                manage_bookstats_frame = "manage_bookstats_frame.glade",
                manage_bookshop_frame = "manage_bookshop_frame.glade",
                manage_product_frame = "manage_product_frame.glade",
                manage_order_frame = "manage_order_frame.glade",
                manage_print_frame = "manage_print_frame.glade",
                add_genre_dialog  = "add_genre_dialog.glade",
                about_dialog = "about_dialog.glade",
                add_print_dialog  = "add_print_dialog.glade")

def get_glade_file(component):
    return os.path.join(GLDFILESDIR, GLDFILES[component])

# UI message errors handling
def _get_dialog(root, message, type):
    """
    Open a dialog to display messages. type must be a gtk dialog type
    e.g. gtk.MESSAGE_ERROR 
    """
    dialog = gtk.MessageDialog(root, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               type, gtk.BUTTONS_OK, str(message))
    dialog.run()
    dialog.destroy()
    
def get_warning_dialog(root, message):
    """
    Open a dialog to display warning messages.
    """
    _get_dialog(root, message, gtk.MESSAGE_WARNING)

def get_error_dialog(root, message):
    """
    Open a dialog to display error messages.
    """
    _get_dialog(root, message, gtk.MESSAGE_ERROR)

def get_save_dialog(root, message):
    """
    Open a dialog to display error messages.
    """
    dialog = gtk.MessageDialog(root, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, message)
    res = dialog.run()
    dialog.destroy()
    return res

def get_info_dialog(root, message):
    """
    Open a dialog to display error messages.
    """
    dialog = gtk.MessageDialog(root, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               gtk.MESSAGE_INFO, gtk.BUTTONS_OK, message)
    dialog.run()
    dialog.destroy()

def mandatory_data_error(name, widget):
    get_warning_dialog(None, "%s is mandatory" % name)
    if widget.get_property('can-focus'):
        widget.grab_focus()

def generic_data_error(message, widget):
    get_warning_dialog(None, message)
    if widget.get_property('can-focus'):
        widget.grab_focus()

# Helper functions to search in treemodels, found in pygtk tutorial.
# TODO: ok, not the best way to do a search ... going to this better soon
def _match_func(row, data):
    column, key = data # data is a tuple containing column number, key
    return row[column] == key

def search(rows, data, func=_match_func):
    """
    Search in rows using func
    
    rows: is a matrix [][]
    input: data is a tuple (column, value).
    func: function used to match value in row[column] 
    """
    if not rows: return None
    for row in rows:
        if func(row, data):
            return row
    return None
